import React from 'react'
import { Link } from 'react-router'
import { Panel, PanelHeader, PanelBody, PanelFooter, Cell, CellBody, CellFooter, CellHeader, Icon, Dialog, Button } from 'react-weui'
export default class UserAddItem extends React.Component {
	constructor(props) {
		super(props)
		this.state = {
            deled: false,
            setted: false,
       		showAuto2: false,
        	style2: {
            	buttons: [
                	{
                    	type: 'default',
                   	 	label: '取消',
                   	 	onClick: this.hideDialog.bind(this)
                	},
                	{
                   		 type: 'primary',
                    	label: '确定',
                    	onClick: this.handleDelAddress.bind(this)
                	}
            	]
        	}
   		}
	}
	hideDialog() {
        this.setState({
            showAuto2: false
        });
    }

    handleDelAddress() {
        this.props.delAddress(this.props.add_data.addressId);
         this.setState({
            showAuto2: false,
            deled: true
        });
    }
    handleSetDefalutAdd() {
        this.props.setDefaultAddress(this.props.add_data.addressId);
        this.setState({
            setted: true
        });
    }
    componentWillReceiveProps(nextProps) {
        if(nextProps.isDeled === true && this.state.deled === true) {
            this.props.fetchAddress();
            this.setState({
                deled: false
            });
        }
        if(nextProps.isSeted === true && this.state.setted === true) {
            this.props.fetchAddress();
            this.setState({
                setted: false
            });
        }
    }
	render() {
        const { storeId, add_data } = this.props; 
		return(
			<Panel>
				<PanelHeader>
					<span>{add_data.name}</span><span style={{ float: 'right'}}>{add_data.phone}</span>
				</PanelHeader>
				<PanelBody>
					<Cell>{add_data.p_name +' '+ add_data.c_name + ' '+ add_data.d_name +' '+ add_data.content}</Cell>
				</PanelBody>
				<PanelFooter href="javascript:void(0);">
					<Cell link>
				     	<CellHeader>默认地址&nbsp;</CellHeader>
        				<CellBody><Icon value="success" style={{color: add_data.is_default?'': '#ccc'}} onClick={this.handleSetDefalutAdd.bind(this)} /></CellBody>
        				<CellFooter >
							<Link to={`/store/${storeId}/user/edit-address/${add_data.addressId}`}><i className="iconfont icon-edit icon" ></i></Link>
							<span>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</span>
							<i className="iconfont icon-delete icon"  onClick={ e=> this.setState({ showAuto2: true}) }></i>
        				</CellFooter>
    				</Cell>
				</PanelFooter>
				<Dialog buttons={this.state.style2.buttons} show={this.state.showAuto2}>
                    确定删除该地址？
                </Dialog>
			</Panel>
		);
	}
}
